package com.dawn.webmagic.file.core.example;

import com.alibaba.fastjson.JSON;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.downloader.PhantomJSDownloader;

import java.io.*;
import java.util.List;

public class JsoupXpath {

    private static Logger logger = LoggerFactory.getLogger(JsoupXpath.class);

    public static void main(String[] args) {
        try {
            String path = "c://temp//html//1234.html";
            File file = new File(path);
            InputStream is = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                stringBuffer.append(line).append("\n");
            }
            String html =  stringBuffer.toString();
            JXDocument jxd = JXDocument.create(html);

//            Document doc = Jsoup.parse(new File(path),"utf-8");
//            JXDocument jxd = JXDocument.create(doc);
//            String str = "//a/b/following-sibling::node()[position()<count(//a/b/following-sibling::node())-count(//a/h/following-sibling::node())]";
            String str = "//a/b/following-sibling::node()[position()<3]";
            List<JXNode> list = jxd.selN(str);
            logger.info("{}", JSON.toJSONString(list));
            String count = "count(//a/b/following-sibling::node())";
            logger.info("{}", jxd.selOne(count));

        } catch (IOException e) {
            logger.error("get file page error", e);
        }
    }

}
